package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventNdkc;
import com.ruoyi.event.domain.bo.EventNdkcBo;
import com.ruoyi.event.domain.vo.EventNdkcVo;
import com.ruoyi.event.mapper.EventNdkcMapper;
import com.ruoyi.event.service.IEventNdkcService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 年度考察Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventNdkcServiceImpl implements IEventNdkcService {

    private final EventNdkcMapper baseMapper;

    /**
     * 查询年度考察
     */
    @Override
    public EventNdkcVo queryById(Long ndkcId) {
        return baseMapper.selectVoById(ndkcId);
    }

    /**
     * 查询年度考察列表
     */
    @Override
    public TableDataInfo<EventNdkcVo> queryPageList(EventNdkcBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventNdkc> lqw = buildQueryWrapper(bo);
        Page<EventNdkcVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询年度考察列表
     */
    @Override
    public List<EventNdkcVo> queryList(EventNdkcBo bo) {
        LambdaQueryWrapper<EventNdkc> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventNdkc> buildQueryWrapper(EventNdkcBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventNdkc> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventNdkc::getUserId, bo.getUserId());
        lqw.eq(bo.getKcks() != null, EventNdkc::getKcks, bo.getKcks());
        lqw.eq(bo.getKcjs() != null, EventNdkc::getKcjs, bo.getKcjs());
        lqw.eq(StringUtils.isNotBlank(bo.getKcdw()), EventNdkc::getKcdw, bo.getKcdw());
        lqw.eq(bo.getKcrq() != null, EventNdkc::getKcrq, bo.getKcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getBrqm()), EventNdkc::getBrqm, bo.getBrqm());
        lqw.eq(StringUtils.isNotBlank(bo.getBrcs()), EventNdkc::getBrcs, bo.getBrcs());
        lqw.eq(StringUtils.isNotBlank(bo.getZylxrqm()), EventNdkc::getZylxrqm, bo.getZylxrqm());
        lqw.eq(StringUtils.isNotBlank(bo.getZylxrcs()), EventNdkc::getZylxrcs, bo.getZylxrcs());
        lqw.eq(StringUtils.isNotBlank(bo.getGzxzkcqk()), EventNdkc::getGzxzkcqk, bo.getGzxzkcqk());
        lqw.eq(bo.getGzxzkcqkrq() != null, EventNdkc::getGzxzkcqkrq, bo.getGzxzkcqkrq());
        lqw.eq(bo.getXybgzjhrq() != null, EventNdkc::getXybgzjhrq, bo.getXybgzjhrq());
        lqw.eq(StringUtils.isNotBlank(bo.getXybgzjh()), EventNdkc::getXybgzjh, bo.getXybgzjh());
        lqw.eq(StringUtils.isNotBlank(bo.getGzzyj()), EventNdkc::getGzzyj, bo.getGzzyj());
        lqw.eq(StringUtils.isNotBlank(bo.getGzzfzr()), EventNdkc::getGzzfzr, bo.getGzzfzr());
        lqw.eq(bo.getGzzyjrq() != null, EventNdkc::getGzzyjrq, bo.getGzzyjrq());
        lqw.eq(StringUtils.isNotBlank(bo.getJdjdb()), EventNdkc::getJdjdb, bo.getJdjdb());
        lqw.eq(StringUtils.isNotBlank(bo.getJdjdbfzr()), EventNdkc::getJdjdbfzr, bo.getJdjdbfzr());
        lqw.eq(bo.getJdjdbrq() != null, EventNdkc::getJdjdbrq, bo.getJdjdbrq());
        return lqw;
    }

    /**
     * 新增年度考察
     */
    @Override
    public Boolean insertByBo(EventNdkcBo bo) {
        EventNdkc add = BeanUtil.toBean(bo, EventNdkc.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setNdkcId(add.getNdkcId());
        }
        return flag;
    }

    /**
     * 修改年度考察
     */
    @Override
    public Boolean updateByBo(EventNdkcBo bo) {
        EventNdkc update = BeanUtil.toBean(bo, EventNdkc.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventNdkc entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除年度考察
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
